package android.mobilin;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.AlertDialog.Builder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ReplyActivity extends Activity implements OnClickListener, android.content.DialogInterface.OnClickListener {
	Button reply;
	EditText recName;
	EditText recAdd;
	EditText recTelFax;
	EditText caseNo;
	EditText hName;
	EditText hDesig;
	TextView pName;
	TextView pAge;
	TextView pSex;
	Builder builder;
	AlertDialog confirm;
	PendingIntent pi;
	DBAdapter db;
	String number;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reply);
		reply=(Button)findViewById(R.id.sendReply);
		reply.setOnClickListener(this);
		recName=(EditText)findViewById(R.id.recv_name);
		recAdd=(EditText)findViewById(R.id.recv_add);
		recTelFax=(EditText)findViewById(R.id.recv_telfax);
		pName=(TextView)findViewById(R.id.p_name);
		pAge=(TextView)findViewById(R.id.p_age);
		pSex=(TextView)findViewById(R.id.p_sex);
		caseNo=(EditText)findViewById(R.id.p_newcaseno);
		hName=(EditText)findViewById(R.id.h_name);
		hDesig=(EditText)findViewById(R.id.h_designation);
		builder = new Builder(this);
		builder.setCancelable(false)
		       .setPositiveButton("Yes", this)
		       .setNegativeButton("Cancel", this);
		String SENT = "SMS_SENT";
        pi = PendingIntent.getBroadcast(this, 0,
            new Intent(SENT), 0);
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "Reply sent", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", 
                                Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", 
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
			
        }, new IntentFilter(SENT));
        db=new DBAdapter(this);
        Bundle b=getIntent().getExtras();
        number=b.getString("refer_number");
        pName.setText(b.getString("p_name"));
        pAge.setText(b.getString("p_age"));
        pSex.setText(b.getString("p_sex"));
	}

	public void onClick(View v) {
		if(v==reply) {
			builder.setMessage("Send reply form?\nSMS charges apply.");
			confirm=builder.show();
		}
	}

	public void onClick(DialogInterface arg0, int arg1) {
		if(arg0==confirm) {
			switch(arg1) {
				case DialogInterface.BUTTON_POSITIVE: 
					String msg="REP\0"+recName.getText().toString()+"\0"
					+recAdd.getText().toString()+"\0"
					+recTelFax.getText().toString()+"\0"
					+pName.getText().toString()+"\0"
					+pAge.getText().toString()+"\0"
					+pSex.getText().toString()+"\0"
					+caseNo.getText().toString()+"\0"
					+hName.getText().toString()+"\0"
					+hDesig.getText().toString();
					StartActivity.sendSMS(number,msg,pi,false);
					break;
				case DialogInterface.BUTTON_NEGATIVE:
					confirm.dismiss();
					break;
			}
		}
	}
}
